/*********************************************************************************************************
**
**                                   GJB 标准测试集
**
**                               Copyright All Rights Reserved
**
**--------------文件信息--------------------------------------------------------------------------------
**
** 文   件   名: gjb_S0100903GN_1.c
**
** 文件创建日期: 2021 年 1 月 22 日
**
** 描        述: 以限时阻塞方式等待条件变量功能测试
*********************************************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <math.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <semaphore.h>
#include <sched.h>

// #include <iostream.h>
// #include <unistd.h>
// #include <sys/time.h>

// #include "gjb.h"

#define TIMEOUT   5

static int err_count_cv_9031;

pthread_cond_t cond_cv_9031 = PTHREAD_COND_INITIALIZER;                         /* cond used by the two threads */

pthread_mutex_t mutex_cv_9031 = PTHREAD_MUTEX_INITIALIZER;                      /* cond used by the two threads */

void * thread_cv_9031(void *tmp)
{
    int             rc = 0;
    struct timespec timeout;
    struct timespec curtime;

    /*
     * acquire the mutex
     */
    rc  = pthread_mutex_lock(&mutex_cv_9031);
    if(rc != 0) {
        printf("test failed.pthread_mutex_lock\n");
        err_count_cv_9031++;
        return (void *)-1;
    }


    if (clock_gettime(CLOCK_REALTIME, &curtime) != 0) {
        printf("Fail to get current time\n");
        err_count_cv_9031++;
        return (void *)-1;
    }

    timeout.tv_sec = curtime.tv_sec;

    timeout.tv_nsec = curtime.tv_nsec;

    timeout.tv_sec += TIMEOUT;

    rc = pthread_cond_timedwait(&cond_cv_9031, &mutex_cv_9031, &timeout);
    if(rc == 0) {
        printf("test pass.pthread_cond_timedwait return 0\n");

    } else {
        printf("test failed,pthread_cond_timedwait return %d",rc);
        err_count_cv_9031++;
        return (void *)-1;
    }

    rc = pthread_mutex_unlock(&mutex_cv_9031);
    if(rc != 0) {
        printf("test failed.pthread_mutex_unlock\n");
        err_count_cv_9031++;
        return (void *)-1;
    }

    return NULL;
}

int gjb_S0100903GN_1(int argc, char **argv)
{
    pthread_t low_id;
    int       rc = 0;

    /*
     * Create a new thread with default attributes
     */
    rc = pthread_create(&low_id, NULL, thread_cv_9031, NULL);
    if(rc != 0) {
        printf("pthread_create\n");
        goto __errno_handle;
    }

    /*
     * Let the other thread run
     */
    sleep(2);

    pthread_cond_signal(&cond_cv_9031);

    if (err_count_cv_9031) {
       goto __errno_handle;
    }

    printf("<RSLT>..................................................[(0)]\n \n \n");

    return  (0);   

__errno_handle:
    printf("<RSLT>..................................................[(-1)]\n \n \n");
    return  (-1);

}
